<template>
  <view>
    <goodsList @goodsItemId="getGoodsId" :goodsList="goodsList"></goodsList>
  </view>
</template>

<script>
import goodsList from "../../compents/goods_list.vue";
export default {
  components: {
    goodsList: goodsList,
  },
  data() {
    return {
      goodsList: [],
      pageIndex: 1,
      queryParams: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 8,
      },
    };
  },
  // 商品数据
  // 当前页码值
  totalPage: 1,
  onLoad: function (o) {
    this.queryParams.cid = o.cid;
    console.log(this.queryParams);
    this.getGoodsList();
  },
  methods: {
    // 获取商品数据
    async getGoodsList() {
      let res = await this.$myRequest({
        url: "/goods/search",
        data: this.queryParams,
      });
      console.log(res);
      let total = res.total;
      this.totalPage = Math.ceil(total / this.queryParams.pagesize);
      console.log(this.totalPage);
      this.goodsList = [...this.goodsList, ...res.goods];
      console.log(res.goods);
    },
    // 点击跳转
    getGoodsId(e) {
      console.log(e);
      uni.navigateTo({
        url: "../detail/detail?goods_id=" + e,
      });
    },
  },
  //   页面触底触发
  onReachBottom() {
    console.log("触底");
    if (this.queryParams.pagenum >= this.totalPage) {
      uni.showToast({
        title: "没有更多数据了",
        icon: "none",
      });
    } else {
      this.queryParams.pagenum++;
      this.getGoodsList();

      console.log(this.queryParams.pagenum);
    }
  },
  // 监听用户下拉刷新
  onPullDownRefresh() {
    console.log(1);
    this.goodsList = [];
    this.queryParams.pagenum = 1;
    this.getGoodsList();
  },
};
</script>

<style>
</style>
